package cn.tom.org.service.impl;

import cn.tom.org.mapper.DepartmentMapper;
import cn.tom.org.pojo.domain.Department;
import cn.tom.org.pojo.query.DepartmentQuery;
import cn.tom.org.service.DepartmentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
@CacheConfig(cacheNames = "deptTree")
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public Department getOne(Long id) {
        return departmentMapper.selectById(id);
    }

    @Override
    public List<Department> getAll() {
        return departmentMapper.selectList(null);
    }

    @Override
    @CacheEvict(key = "'tree'")
    @Transactional
    public void add(Department department) {
        departmentMapper.insert(department);
    }

    @Override
    @CacheEvict(key = "'tree'")
    @Transactional
    public void update(Department department) {
        departmentMapper.updateById(department);
    }

    @Override
    public PageInfo<Department> queryPage(DepartmentQuery query) {
        PageHelper.startPage(query.getCurrentPage(),query.getPageSize());
        List<Department> list =departmentMapper.quaryPage(query);
        return new PageInfo<Department>(list);
    }

    @Override
    @CacheEvict(key = "'tree'")
    @Transactional
    public void delete(Long id) {
        departmentMapper.deleteById(id);
    }

    @Override
    @CacheEvict(key = "'tree'")
    @Transactional
    public void patchDelete(List<Long> ids) {
        departmentMapper.deleteBatchIds(ids);
    }


    @Override
    @Cacheable(key = "'tree'")
    public List<Department> deptTree() {
        return getDeptTree();
    }
    //从数据库中查询部门树
    private List<Department> getDeptTree() {
        List<Department> departments = departmentMapper.selectList(null);
        //将所有的部门查询出来装到Map中，相当于放在内存中，方便等会通过id去取对象
        Map<Long, Department> map = new HashMap<>();
        for (Department department : departments) {
            map.put(department.getId(),department);
        }

        List<Department> deptTree = new ArrayList<>();
        for (Department department : departments) {
            if(department.getParentId()==null){//顶级部门
                deptTree.add(department);
            }else{
                //获取父部门的id
                Long parentId = department.getParentId();
                //获取父部门的对象
                Department parentDepartment = map.get(parentId);
                if(parentDepartment!=null){
                    //将自己状态父部门的集合
                    List<Department> children = parentDepartment.getChildren();
                    children.add(department);
                }
            }
        }
        return deptTree;
    }
}
